<?php
	
require '../include/user_login.inc.php';
require '../include/paging.inc.php';

if($_SESSION['ACCESS_SID'] != 1) redirect(HTML_BASE);

$user_id = $_SESSION['USER_SID'];

if ($_POST['is_submitted']) {
		$error_message = '';
		
		if (!$_POST['title']) $error_message .= 'Не е въведенa Тема<br/>';
		
		if (!$error_message) {
			$sql = "INSERT INTO tasks
						 (title,priority,status,by_user)
					   VALUES
						 ('".escape($_POST['title'])."','".$_POST['priority']."','1','".$user_id."')
					   
					";

				query($sql);
				$id = mysql_insert_id();
				redirect(HTML_BASE.$_SESSION["lang"].'/correspondence/'.$id);
				
			
		}
	}
	



$sql = "SELECT
			task_id,
			title,
			priority,
			status,
			creation_time
		FROM
			tasks
		WHERE
			by_user = '$user_id'
			
			ORDER BY  creation_time DESC
			LIMIT 10
		";



$result = query($sql);
	
	head('User Dashboard');
	menu_user();

?>


		
	<?php if ($_SESSION['message']) { ?>
	<div class="message green">
		<?=$_SESSION['message']?>
	</div>
	<?php unset($_SESSION['message']); } ?>
	
	<?php if ($error_message) { ?>
	<div class="message red">
		<?=$error_message?>
	</div>
	<?php } ?>
		
		
	<div id="newtask">	
		
		<form action="<?=HTML_BASE.$_SESSION["lang"]."/dashboard/"?>" method="post" enctype="multipart/form-data">
		<input type="hidden" name="is_submitted" value="1" />
		
		<h4><?=__NEW_CORRESPONDECE?></h4><br>
		
		<label><?=__TITLE?></label>
		<input class="inputbox" name="title" type="text" >
		
		<input type="submit" name="submit" id="submit" class="button" value="<?=__START?>">
		
		<br>
		
	
		<label>
		<input type="checkbox" name="priority" value="1"> <?=__MARK_AS_URGENT?></label>
		
		
		</form>
		
	</div>
	
	<div id="content">
		
		<div id="left">

		<h3><?=__correspondences?></h3>
		
		<table width="99%" cellpadding="0" cellspacing="0">
		<thead>
			<tr>
			
				<td><?=__topic?></td>
                
                <td><?=__date?></td>
				
				
			</tr>
		</thead>
		<tbody>
		<?php
		  $i=1;
		  while ($row = mysql_fetch_object($result)) {				  
		?>
				<tr class="<?=(($i%2)?"none":"zebra")?>">
					
					<td style="border-left:4px solid <?=($row->priority == 1)?'#F30"':'#666'?> ">					
					
						<a href="<?=HTML_BASE.$_SESSION["lang"]?>/correspondence/<?=$row->task_id?>" title="Преглед"><?=sth($row->title)?></a>
					</td>
					
					
					
					<td class="switch">
						<?=date('d M Y', strtotime($row->creation_time))?>	
					</td>
					
					
				</tr>
         <?php
              $i++;    
            } 
         ?>				  
			</tbody>
		</table>
        
		</div>
		
		<div id="right">
		
		<h3><?=__tasks?></h3>
        
      	<?php
		$sql = "SELECT
				n.note_id,
				n.title,
				n.priority,
				n.status,
				n.by_user,
				n.start_date,
				n.end_date
			FROM
				notes AS n
			WHERE
				
				n.by_user = '$user_id'
				
			
			ORDER BY n.start_date	DESC LIMIT 10
			";
 			
			$result = query($sql);
			?>
            
            <table id="listing" width="99%" cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                
                    <td><?=__task?></td>
                    
                    <td><?=__date?></td>
                    
                    
                </tr>
            </thead>
            <tbody>
            <?php
		  $i=1;
			  
			  while ($row = mysql_fetch_object($result)) {				  
			?>
            	
                
                <tr class="<?=(($i%2)?"none":"zebra")?>">
					
					<td style="border-left:4px solid <?=($row->priority == 1)?'#F30"':'#666'?> ">					
					
						<a href="<?=HTML_BASE.$_SESSION["lang"]?>/task/<?=$row->note_id?>" title="<?=__view?>"><?=sth($row->title)?></a>
					</td>
					
					
					
					<td class="switch">
						<?=date('d M Y', strtotime($row->end_date))?>	
					</td>
					
					
				</tr>
                
                
                
          
         <?php
              $i++;   
            } 
         ?>	    
		 </tbody>
		</table>
		 </div> 	
			
			
   </div>
    

<?php
foot();
?>